package com.itboy.mall.ums.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.itboy.common.utils.PageUtils;
import com.itboy.common.core.page.PagePlus;
import com.itboy.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.itboy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itboy.mall.ums.member.bo.MallMemberAddBo;
import com.itboy.mall.ums.member.bo.MallMemberQueryBo;
import com.itboy.mall.ums.member.bo.MallMemberEditBo;
import com.itboy.mall.ums.member.domain.MallMember;
import com.itboy.mall.ums.member.mapper.MallMemberMapper;
import com.itboy.mall.ums.member.vo.MallMemberVo;
import com.itboy.mall.ums.member.service.IMallMemberService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 会员管理Service业务层处理
 *
 * @author itboy
 * @date 2021-09-21
 */
@Service
public class MallMemberServiceImpl extends ServicePlusImpl<MallMemberMapper, MallMember> implements IMallMemberService {

    @Override
    public MallMemberVo queryById(Long id){
        return getVoById(id, MallMemberVo.class);
    }

    @Override
    public TableDataInfo<MallMemberVo> queryPageList(MallMemberQueryBo bo) {
        PagePlus<MallMember, MallMemberVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MallMemberVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MallMemberVo> queryList(MallMemberQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MallMemberVo.class);
    }

    private LambdaQueryWrapper<MallMember> buildQueryWrapper(MallMemberQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallMember> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getLevelId() != null, MallMember::getLevelId, bo.getLevelId());
        lqw.like(StrUtil.isNotBlank(bo.getUsername()), MallMember::getUsername, bo.getUsername());
        lqw.like(StrUtil.isNotBlank(bo.getNickname()), MallMember::getNickname, bo.getNickname());
        lqw.like(StrUtil.isNotBlank(bo.getMobile()), MallMember::getMobile, bo.getMobile());
        lqw.eq(bo.getStatus() != null, MallMember::getStatus, bo.getStatus());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(MallMemberAddBo bo) {
        MallMember add = BeanUtil.toBean(bo, MallMember.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(MallMemberEditBo bo) {
        MallMember update = BeanUtil.toBean(bo, MallMember.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MallMember entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
